<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>arrToString</title>
  <script>
function bestTimetoBuy(){
var price = [2,4,2,6,5,1,20];
var min = Number.MAX_VALUE;
var maxProfit = Number.MIN_VALUE;
for (var i = 0; i < price.length; i++){
  if (price[i] < min)
    min = price[i];
  if (price[i] - min > maxProfit)
    maxProfit = price[i] - min;
  }
  return maxProfit;
}
console.log(bestTimetoBuy());
  </script>
</head>
<body>
  
</body>
</html>